package com.ruoyi.develop.lvFapiao.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.develop.goodsSalebuy.domain.GoodsSalebuy;
import com.ruoyi.develop.goodsSalebuy.service.IGoodsSalebuyService;
import com.ruoyi.develop.goodsSalebuyall.domain.GoodsSalebuyall;
import com.ruoyi.develop.goodsSalebuyall.service.IGoodsSalebuyallService;
import com.ruoyi.develop.lvBuy.domain.LvBuy;
import com.ruoyi.develop.lvBuy.service.ILvBuyService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.lvFapiao.domain.LvFapiao;
import com.ruoyi.develop.lvFapiao.service.ILvFapiaoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 到货发票Controller
 * @author qh
 * @date 2024-09-14
 */
@RestController
@RequestMapping("/lvFapiao/lvFapiao")
public class LvFapiaoController extends BaseController {
    @Autowired
    private ILvFapiaoService lvFapiaoService;

    @Autowired
    private ILvBuyService lvBuyService;

    @Autowired
    private IGoodsSalebuyallService goodsSalebuyallService;

    @Autowired
    private IGoodsSalebuyService goodsSalebuyService;
    /**
     * 查询到货发票列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFapiao:query')")
    @GetMapping("/list")
    public TableDataInfo list(LvFapiao entity) {
        startPage();
        List<LvFapiao> list = lvFapiaoService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出到货发票列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFapiao:export')")
    @Log(title = "到货发票", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LvFapiao entity) {
        List<LvFapiao> list = lvFapiaoService.selectList(entity);
        ExcelUtil<LvFapiao> util = new ExcelUtil<LvFapiao>(LvFapiao. class);
        util.exportExcel(response, list, "到货发票数据");
    }

    /**
     * 获取到货发票详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFapiao:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(lvFapiaoService.selectById(id));
    }

    /**
     * 新增到货发票
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFapiao:add')")
    @Log(title = "到货发票", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LvFapiao entity) {
        return toAjax(lvFapiaoService.insert(entity));
    }

    @PostMapping("/addFp")
    public AjaxResult addFp(@RequestBody LvFapiao entity) {
        return toAjax(lvFapiaoService.insertFp(entity));
    }
    /**
     * 修改到货发票
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFapiao:edit')")
    @Log(title = "到货发票", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody LvFapiao entity) {
        return toAjax(lvFapiaoService.update(entity));
    }

    /**
     * 删除到货发票
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFapiao:remove')")
    @Log(title = "到货发票", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(lvFapiaoService.deleteByIds(ids));
    }

    @DeleteMapping("/deleteById/{id}")
    public AjaxResult deleteById(@PathVariable String id) {
        LvFapiao lvFapiao=lvFapiaoService.selectById(id);
        LvBuy lvBuy=lvBuyService.selectById(lvFapiao.getCaigouid());
        lvBuy.setKaipiao(lvBuy.getKaipiao().subtract(lvFapiao.getKaipiao()));
        lvBuy.setShengyukp(lvBuy.getMoney().add(lvFapiao.getKaipiao()));
        lvBuyService.update(lvBuy);
        List<GoodsSalebuyall> goodsSalebuyalls=goodsSalebuyallService.selectMainId(lvFapiao.getCaigouid());
        for(GoodsSalebuyall goodsSalebuyall:goodsSalebuyalls){
            if(goodsSalebuyall.getFapiaoId().contains("," + lvFapiao.getId())){
                goodsSalebuyall.setFapiaoId(goodsSalebuyall.getFapiaoId().replace("," + lvFapiao.getId(), ""));
            }else{
                goodsSalebuyall.setFapiaoId(goodsSalebuyall.getFapiaoId().replace(lvFapiao.getId(),""));
            }
            if(goodsSalebuyall.getFapiaono().contains("," + lvFapiao.getNos())){
                goodsSalebuyall.setFapiaono(goodsSalebuyall.getFapiaono().replace("," + lvFapiao.getNos(), ""));
            }else{
                goodsSalebuyall.setFapiaono(goodsSalebuyall.getFapiaono().replace(lvFapiao.getNos(), ""));
            }
            goodsSalebuyallService.update(goodsSalebuyall);
            List<GoodsSalebuy> goodsSalebuyList=goodsSalebuyService.selectByCgallId(goodsSalebuyall.getId());
            for(GoodsSalebuy goodsSalebuy:goodsSalebuyList){
                if(goodsSalebuy.getFapiaoId().contains("," + lvFapiao.getId())) {
                    goodsSalebuy.setFapiaoId(goodsSalebuy.getFapiaoId().replace("," + lvFapiao.getId(), ""));
                }else{
                    goodsSalebuy.setFapiaoId(goodsSalebuy.getFapiaoId().replace(lvFapiao.getId(),""));
                }
                if(goodsSalebuy.getFapiaono().contains("," + lvFapiao.getNos())) {
                    goodsSalebuy.setFapiaono(goodsSalebuy.getFapiaono().replace("," + lvFapiao.getNos(), ""));
                }else{
                    goodsSalebuy.setFapiaono(goodsSalebuy.getFapiaono().replace(lvFapiao.getNos(), ""));
                }
                goodsSalebuyService.update(goodsSalebuy);
            }
        }
        return toAjax( lvFapiaoService.deleteById(id));
    }
}
